/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog.calendar;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.ui.dialog.base.StatusBarDialog;
import com.ecarx.systemui.plugin.utils.LogUtils;
import java.util.Locale;

public class CalendarTimeDialog extends StatusBarDialog {

    private static final String TAG = "CalendarTimeDialog";

    public static final String LANGUAGE_CN = "zh";


    public CalendarTimeDialog(Context context) {
        super(context, R.style.CommonDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d(TAG, "--onCreate--");
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        if (window != null) {
            int type = DialogManager.TYPE_VOLUME_OVERLAY;
            window.requestFeature(Window.FEATURE_NO_TITLE);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND
                    | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
//            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
//                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            window.setType(type);
            window.setWindowAnimations(com.android.internal.R.style.Animation_Toast);

            final WindowManager.LayoutParams lp = window.getAttributes();
            lp.format = PixelFormat.TRANSLUCENT;
            lp.gravity = Gravity.END | Gravity.TOP;
            lp.windowAnimations = -1;
            lp.x = 40;
            lp.y = 104;
            window.setAttributes(lp);
/*            String language = Locale.getDefault().getLanguage();
            if (isCn(language)) {
                setContentView(R.layout.dialog_calendar_time);
                initViewCn();
            } else {
                setContentView(R.layout.dialog_calendar_time_eu);
                initViewEu();
            }*/
            //TODO YJJ 目前HY11还没有EU版本，先直接用CN的
            setContentView(R.layout.dialog_calendar_time);
            initViewCn();
        }
    }

    private void initViewCn() {
        String xcTheme = Settings.Global.getString(getContext().getContentResolver(), "XCTheme");
        if(TextUtils.isEmpty(xcTheme)){
            xcTheme = "Orange";
        }
        TextView tvDateDay = findViewById(R.id.tv_date_day);
        TextView tvDateWeek = findViewById(R.id.tv_date_week);
        TextView tvDateYear = findViewById(R.id.tv_date_year);
        TextView tvWeekNum = findViewById(R.id.tv_week_num);

        TextView tvYearCn = findViewById(R.id.tv_year_cn);
        TextView tvTwelveAnimals = findViewById(R.id.tv_twelve_animals);
        TextView tvDateCnDay = findViewById(R.id.tv_date_cn_day);
        TextView tvFestival = findViewById(R.id.tv_festival);

        LinearLayout llTopWeek = findViewById(R.id.ll_top_week);
        RecyclerView rvCalendar = findViewById(R.id.rv_calendar);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),7){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rvCalendar.setLayoutManager(gridLayoutManager);
        CalendarAdapter calendarAdapter = new CalendarAdapter();
        rvCalendar.setAdapter(calendarAdapter);
        CalendarData calendarData = CalendarData.getInstance().loadData();
        tvDateDay.setText(calendarData.getStrDateDay());
        tvDateYear.setText(calendarData.getStrYear());
        tvDateWeek.setText(calendarData.getCnWeek());
        tvWeekNum.setText(calendarData.getStrWeekNum());
        tvYearCn.setText(calendarData.getTrunkBranchYear());
        tvTwelveAnimals.setText(calendarData.getTwelveAnimal());
        tvDateCnDay.setText(calendarData.getNLMonthAndDay());
        llTopWeek.setVisibility(View.VISIBLE);
        calendarAdapter.setData(calendarData.getCalendarItemEntities());
        String currentFestival = calendarData.getCurrentFestival();
        if(!TextUtils.isEmpty(currentFestival)){
            tvFestival.setTextColor(getThemeTextColor(xcTheme));
            tvFestival.setVisibility(View.VISIBLE);
            tvFestival.setText(currentFestival);
        }
        LogUtils.d(TAG,"initViewCn calendarData:"+calendarData.toString());
    }


    private void initViewEu() {

        TextView tvWeekDay = findViewById(R.id.tv_week_day);
        TextView tvWeekNumYear = findViewById(R.id.tv_week_num_year);
        LinearLayout llTopWeek = findViewById(R.id.ll_top_week);
        RecyclerView rvCalendar = findViewById(R.id.rv_calendar);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),7){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rvCalendar.setLayoutManager(gridLayoutManager);
        CalendarEnAdapter calendarAdapter = new CalendarEnAdapter();
        rvCalendar.setAdapter(calendarAdapter);
        CalendarEnData calendarEnData = CalendarEnData.getInstance().loadData();
        tvWeekDay.setText(String.format(Locale.getDefault(),"%s, %s",
                calendarEnData.getEnStrWeek(), calendarEnData.getEnStrMonthDay()));
        tvWeekNumYear.setText(String.format(Locale.getDefault(),"%s, %d",
                calendarEnData.getStrWeekNum(), calendarEnData.getYear()));
        llTopWeek.setVisibility(View.VISIBLE);
        calendarAdapter.setData(calendarEnData.getCalendarItemEntities());
        LogUtils.d(TAG,"initViewEu calendarEnData:"+calendarEnData.toString());
    }

    private int getThemeTextColor(String xcTheme){
        int color;
        switch (xcTheme) {
            case "Pink":
                color = getContext().getColor(R.color.text_theme_pink);
                break;
            case "Red":
                color = getContext().getColor(R.color.text_theme_red);
                break;
            case "Yellow":
                color = getContext().getColor(R.color.text_theme_yellow);
                break;
            case "Edition1":
                color = getContext().getColor(R.color.text_theme_edition1);
                break;
            case "Blue":
                color = getContext().getColor(R.color.text_theme_blue);
                break;
            default:
                color = getContext().getColor(R.color.text_theme_orange);
                break;
        }
        return color;
    }

    @Override
    public void show() {
        super.show();
    }


    @Override
    protected void onStart() {
        super.onStart();
        startTimingClose();
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelTimingClose();
    }

    private boolean isCn(String language) {
        return LANGUAGE_CN.equalsIgnoreCase(language);
    }
}
